#include "FrameMgr.h"
USING_NS_CC;

FrameMgr::FrameMgr(void)
	:_layerNum(3)
{
	_gameScene = Scene::create();

	for (int i=0; i<_layerNum; i++)
	{
		Layer* layer = Layer::create();
		_layerList.push_back(layer);

		_gameScene->addChild(layer);
	}
	auto director = Director::getInstance();
	director->runWithScene(_gameScene);
}


FrameMgr::~FrameMgr(void)
{
	disposeAll();
}

bool FrameMgr::openFrame( std::string frameName, void* data /*= nullptr*/ )
{
	GameFrame* frame = getFrame(frameName);

	if (frame == nullptr)
	{ 
		frame = _reflection.create(frameName); 
		if(frame)
		{
			_frameList.insert(std::make_pair(frameName, frame));
			_showFrameList.insert(std::make_pair(frameName, false));
		}
	}

	if (frame == nullptr){ return false; }

	frame->show(data);
	showFrame(frameName, frame, true);

	return true;
}

bool FrameMgr::closeFrame( std::string frameName )
{
	GameFrame* frame = getFrame(frameName);

	if (frame == nullptr){ return false; }

	showFrame(frameName, frame, false);
	frame->close();

	return true;
}


GameFrame* FrameMgr::getFrame( std::string frameName )
{
	auto iter = _frameList.find(frameName);
	if (iter != _frameList.end())
	{
		return iter->second;
	}
	return nullptr;
}

bool FrameMgr::frameIsShow( std::string key )
{
	return _showFrameList[key];
}


void FrameMgr::showFrame( std::string strKey, GameFrame* frame, bool b )
{
	int layerIndex		= _frameLayerList[strKey];
	Layer* layer		= _layerList[layerIndex];
	bool bShow			= _showFrameList[strKey];

	if (frame->getLayer() != nullptr && b && !bShow)
	{
		layer->addChild(frame->getLayer());
		_showFrameList[strKey] = true;
	}
	else if(frame->getLayer() != nullptr && !b && bShow )
	{
		layer->removeChild(frame->getLayer());
		_showFrameList[strKey] = false;
	}
}

void FrameMgr::dispose( std::string frameName )
{
	auto iter = _frameList.find(frameName);

	if (iter != _frameList.end())
	{
		delete iter->second;
		_frameList.erase(iter);
	}
}

void FrameMgr::disposeAll()
{
	auto iter = _frameList.begin();
	for (; iter!=_frameList.end(); iter++)
	{
		delete iter->second;
	}
	_frameList.clear();
}

bool FrameMgr::addNodeToScene( cocos2d::Node* node )
{
	cocos2d::Array* arr = _gameScene->getChildren();
	long index = arr->getIndexOfObject(node);
	if( index != CC_INVALID_INDEX )
		return false;

	_gameScene->addChild(node);
	return true;
}

bool FrameMgr::removeNodeToScene( cocos2d::Node* node )
{
	_gameScene->removeChild(node);
	return true;
}

bool FrameMgr::addNodeToLayer( cocos2d::Node* node, int layerIndex )
{
	layerIndex = getLayerIndex(layerIndex);

	Layer* layer		= _layerList[layerIndex];

	if (layer == nullptr) return false;

	cocos2d::Array* arr = layer->getChildren();
	long index = arr->getIndexOfObject(node);
	if( index != CC_INVALID_INDEX )
		return false;

	layer->addChild(node);
	return true;
}

bool FrameMgr::removeNodeToLayer( cocos2d::Node* node, int layerIndex )
{
	layerIndex = getLayerIndex(layerIndex);
	Layer* layer		= _layerList[layerIndex];
	if (layer == nullptr) return false;

	layer->removeChild(node);
	return true;
}


